package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PermissionDao;
import com.itheima.domain.Permission;
import com.itheima.service.PermissionService;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.UUID;

public class PermissionServiceImpl implements PermissionService {

    @Override
    public void save(Permission permission) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PermissionDao mapper = sqlSession.getMapper(PermissionDao.class);
        permission.setId(UUID.randomUUID().toString());
        mapper.save(permission);
        sqlSession.close();
    }

    @Override
    public void update(Permission permission) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PermissionDao mapper = sqlSession.getMapper(PermissionDao.class);
        mapper.update(permission);
        sqlSession.close();
    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PermissionDao mapper = sqlSession.getMapper(PermissionDao.class);
        mapper.delete(id);
        sqlSession.close();
    }

    @Override
    public Permission findById(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PermissionDao mapper = sqlSession.getMapper(PermissionDao.class);
        Permission permission = mapper.findById(id);
        sqlSession.close();
        return permission;
    }

    @Override
    public List<Permission> findAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PermissionDao mapper = sqlSession.getMapper(PermissionDao.class);
        List<Permission> permissions = mapper.findAll();
        sqlSession.close();
        return permissions;
    }

    @Override
    public PageInfo findAll(int page, int size) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PermissionDao mapper = sqlSession.getMapper(PermissionDao.class);
        PageHelper.startPage(page, size);
        List<Permission> permissions = mapper.findAll();
        PageInfo<Permission> PageInfo = new PageInfo<>(permissions);
        sqlSession.close();
        return PageInfo;
    }
    ////通过角色ID查找对应权限
    @Override
    public List<Map> findAuthorDataByRoleId(String roleId) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PermissionDao mapper = sqlSession.getMapper(PermissionDao.class);
        return mapper.findAuthorDataByRoleId(roleId);
    }
}
